﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Doo_JetDAL;
using DoodJetView.Core;

namespace DoodJetView.VuesPartielles.Reservation
{
    /// <summary>
    /// Logique d'interaction pour ListReservation.xaml
    /// </summary>
    public partial class ListReservation : UserControl
    {
        private MainWindow mainWindow{get;set;}

        public ListReservation()
        {
            InitializeComponent();
        }
        public ListReservation(MainWindow mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
            this.LoadSelectClient(string.Empty);
        }
        public void LoadSelectClient(string Recherche) 
        {
            using (DataEntities db = new DataEntities())
            {
                List<client> lsite = null;
                if (Recherche == String.Empty)
                { lsite = db.client.ToList(); }
                else
                {
                    lsite = db.client.Where(o=>o.nomClient.Contains(Recherche)).ToList();
                }

                if (lsite.Count > 0)
                    list.Items.Clear();

                foreach (client item in lsite)
                {
                    list.Items.Add(new ElementListPoco { id = (int)item.id_client, text = item.nomClient + " " + item.prenomClient + " ("+ DateTimeUnixTools.ConvertFromUnixTimestamp( item.dateNaissance).ToString("dd/MM/yyyy") + ")"});
                }
            }
        }

        private void SaisieTexteRecherche(object sender, KeyEventArgs e)
        {
            LoadSelectClient(txt_recherhche.Text);
        }

        private void ClientSelectionner(object sender, SelectionChangedEventArgs e)
        {
            RafraichirSelectReservation();
        }

        private void RafraichirSelectReservation()
        {
            if (list.SelectedItem == null) return;
            using (DataEntities db = new DataEntities())
            {
                long id = long.Parse(((ElementListPoco)list.SelectedItem).id.ToString());
                List<reservation> lst = db.reservation.Where(o => o.id_client == id).ToList();
                listReserv.Items.Clear();
                foreach (reservation item in lst)
                {
                    listReserv.Items.Add(new ElementListPoco { id = (int)item.id_reservation, text = DateTimeUnixTools.ConvertFromUnixTimestamp(item.dateDebut).ToString("dd/MM/yyyy à hh:mm") + " " + item.client.nomClient + item.equipements.First().TypeEquipement.libelle });
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
                //( ElementListPoco)listReserv.SelectedItem
                try
                {
                    using (DataEntities db = new DataEntities())
                    {
                        long id = long.Parse(SelectedRes.Text);
                        reservation del = db.reservation.SingleOrDefault(o => o.id_reservation == id);
                        if (del != null)
                        {
                            db.DeleteObject(del);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
                finally
                {
                    RafraichirSelectReservation();
                }
              
        }

        private void listReserv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (((ElementListPoco)((ListBox)sender).SelectedItem).id != null && ((ElementListPoco)((ListBox)sender).SelectedItem).id != 0)
                {
                    SelectedRes.Text = ((ElementListPoco)((ListBox)sender).SelectedItem).id.ToString();
                    RafraichirSelectReservation();
                }
            }
            catch (Exception ex)
            { }
        }
    }
}
